package com.ouli.crowd.mvc.handler;

import com.github.pagehelper.PageInfo;
import com.ouli.crowd.constant.CrowdConstant;
import com.ouli.crowd.entity.Admin;
import com.ouli.crowd.service.api.AdminService;
import com.ouli.crowd.util.CrowdUtil;
import com.ouli.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class AdminHandler {

    @Autowired
    private AdminService adminService;

    @ResponseBody
    @RequestMapping("/admin/add/info.json")
    public ResultEntity addAdmin(@RequestParam("adminLoginAcct") String adminLoginAcct,
                                 @RequestParam("adminName") String adminName,
                                 @RequestParam("email") String email,
                                 @RequestParam("adminPswd") String adminPswd) {
        adminService.saveAdmin(new Admin(null,adminLoginAcct,CrowdUtil.md5(adminPswd),adminName,email,null));
        return ResultEntity.successWithoutData();
    }

    @RequestMapping("admin/editRedirect/{adminId}.html")
    public String editRedirectAdmin(@PathVariable("adminId") Integer adminId,
                                    ModelMap modelMap) {
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_EDIT_ADMIN,adminService.getAdminById(adminId));

        return "admin-edit";

    }

    @RequestMapping("admin/save/page.html")
    public String saveAdmin(@RequestParam("adminLoginAcct") String adminLoginAcct,
                            @RequestParam("adminName") String adminName,
                            @RequestParam("email") String email,
                            @RequestParam("adminPswd") String adminPswd) {
        adminService.saveAdmin(new Admin(null,adminLoginAcct, CrowdUtil.md5(adminPswd),adminName,email,null));
        return "redirect:/admin/get/page.html";
    }

    @RequestMapping("admin/delete/{adminId}/{pageNum}/{keyword}.html")
    public String deleteAdmin(@PathVariable("adminId") Integer adminId,
                              @PathVariable("keyword") String keyword,
                              @PathVariable("pageNum") Integer pageNum) {
        adminService.deleteAdminById(adminId);

        // 尝试方案1，直接转发到admin-page，jsp无法显示分页数据
        // return "admin-page";
        return "redirect:/admin/get/page.html?keyword="+ keyword+"&pageNum="+pageNum;
    }

    @RequestMapping("admin/get/page.html")
    public String getPageInfo(
                              // 使用@RequestParam注解的defaultValue属性，指定默认值，在请求中没有携带对应参数时使用默认值
                              // keyword默认使用空字符串，和SQL语句配合实现两种情况适配
                              @RequestParam(value = "keyword",defaultValue = "")String keyword,
                              // pageNum默认值使用1
                              @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                              // pageSize默认值使用5
                              @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                              ModelMap modelMap
                              ) {
        // 调用service方法获取PageInfo
        PageInfo<Admin> pageInfo = adminService.getAdminByKeyWord(keyword,pageNum,pageSize);
        // 将PageInfo对象存入模型
            modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO,pageInfo);
            modelMap.addAttribute("keyword",keyword);
            return "admin-page";
    }

    @RequestMapping("admin/do/logout.html")
    public String doLogout(HttpSession session) {

        //强制session失效
        session.invalidate();
        return "redirect:/admin/to/login/page.html";
    }

    @RequestMapping("/admin/do/login.html")
    public String doLogin(@RequestParam("loginAcct") String loginAcct,
                          @RequestParam("userPswd") String userPswd,
                          HttpSession session) {
        // 调用service方法进行登陆检查
        //这个方法能够返回admin对象，说明登陆成功，如果正好密码不正确，则会抛出异常
        Admin admin = adminService.getAdminByLoginAcct(loginAcct,userPswd);

        //将登陆成功返回的admin对象存入Session域
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN,admin);
        //这样转发会导致页面刷新时重复提交登陆的表单，所以不合适
        //return "admin-main";
        return "redirect:/admin/to/main/page.html";
    }

}
